package com.wss.common.manage;

import com.wss.common.base.bean.BaseBean;
import com.wss.common.bean.Template;
import com.wss.common.constants.Constants;
import com.wss.common.constants.Dic;
import com.wss.common.utils.NetworkUtil;
import com.wss.common.utils.ToastUtils;
import com.wss.common.utils.Utils;
import com.wss.common.view.browser.BrowserAbility;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AbilityToAbility {

    ////////////////////////////////////////普通Activity跳转////////////////////////////////////////

    /**
     * 普通Ability之间跳转
     * @param ability
     * @param abilityName 目标ability name
     */
    public static void toAbility(Context ability, String abilityName) {
        toAbilityForResult(ability, abilityName, null, 0);
    }

    public static void toAbility(Context ability, Class toAbility) {
        toAbilityForResult(ability, toAbility, null, 0);
    }

    /**
     * 普通Ability之间跳转
     *
     * @param ability
     * @param abilityName 目标ability name
     * @param params 携带参数
     */
    public static void toAbility(Context ability, String abilityName, Map<String, ?> params) {
        toAbilityForResult(ability, abilityName, params, 0);
    }

    public static void toAbility(Context ability, Class toAbility, Map<String, ?> params) {
        toAbilityForResult(ability, toAbility, params, 0);
    }

    /**
     * 普通Ability之间跳转
     *
     * @param ability
     * @param abilityName 目标ability name
     * @param requestCode 请求码  需大于0
     */
    public static void toAbilityForResult(Context ability, String abilityName, int requestCode) {
        toAbilityForResult(ability, abilityName, requestCode);
    }

    public static void toAbilityForResult(Context context,
                                          String abilityName,
                                          Map<String, ?> params,
                                          int requestCode) {

        Intent intent = new Intent();

        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(context.getBundleName())
                .withAbilityName(abilityName)
                .build();

        intent.setOperation(operation);
        assembleParams(intent, params);
        context.startAbility(intent, requestCode);
    }

    public static void toAbilityForResult(Context context,
                                          Class ability,
                                          Map<String, ?> params,
                                          int requestCode) {

        Intent intent = new Intent();

        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(context.getBundleName())
                .withAbilityName(ability)
                .build();

        intent.setOperation(operation);
        assembleParams(intent, params);
        context.startAbility(intent, requestCode);
    }

    private static void assembleParams(Intent intent, Map<String, ?> params) {
        if (params != null) {
            for (Map.Entry<String, ?> entry : params.entrySet()) {
                String key = entry.getKey();
                Object value = params.get(key);
                if (value instanceof String) {
                    intent.setParam(key, (String) value);
                } else if (value instanceof Boolean) {
                    intent.setParam(key, (boolean) value);
                } else if (value instanceof Integer) {
                    intent.setParam(key, (int) value);
                } else if (value instanceof Float) {
                    intent.setParam(key, (float) value);
                } else if (value instanceof Double) {
                    intent.setParam(key, (double) value);
                } else if (value instanceof Long) {
                    intent.setParam(key, (long) value);
                } else if (value instanceof Short) {
                    intent.setParam(key, (short) value);
                } else if (value instanceof IntentParams) {
                    intent.setParam(key, (IntentParams) value);
                } else if (value instanceof BaseBean) {
                    intent.setParam(key, (BaseBean) value);
                } else if (value instanceof ArrayList) {
                    intent.setParam(key, (ArrayList) value);
                } else if (value instanceof HashMap) {
                    intent.setParam(key, (HashMap) value);
                }
            }
        }
    }

    //////////////////////////////////////WebView跳转//////////////////////////////////////

    /**
     * 跳转WebView url 不可为空
     *
     * @param context context
     * @param url     链接
     * @param title   标题
     */
    public static void toWebView(Context context, String url, String title) {

        if (!NetworkUtil.isLink(url)) {
            return;
        }
        Map<String, String> param = new HashMap<>();
        param.put(Dic.URL, url);
        param.put(Dic.TITLE_TEXT, title);
        toAbility(context, BrowserAbility.class, param);
    }

    /**
     * 跳转WebView url 不可为空
     *
     * @param context 链接
     * @param url     标题
     */
    public static void toWebView(Context context, String url) {
        toWebView(context, url, "");
    }

    /**
     * 通过模板跳转Activity
     *
     * @param ability activity
     * @param template 模板信息
     */
    @SuppressWarnings("unchecked")
    public static void toAbility(Ability ability, Template template) {
        switch (template.getType()) {
            case Constants.TemplateType.ACTIVITY:
                //跳转Activity
                toAbility(ability, template.getClazz().getName(), template.getParams());
                break;
            case Constants.TemplateType.AROUTER:
                //跳转Arouter
//                toActivity(activity, template.getUrl(), template.getParams());
                break;
            case Constants.TemplateType.WEB_VIEW:
                //跳转WebView
                toWebView(ability, template.getUrl());
                break;
            case Constants.TemplateType.SYS_BROWSER:
                //跳转手机浏览器
                Utils.toSystemBrowser(ability, template.getUrl());
                break;
            default:

        }
    }

    /**
     * 跳转登录页
     *
     * @param context ctx
     */
    public static void toLoginActivity(Context context) {
        ToastUtils.show("跳转登录页");
    }
}
